package com.xmb.wechat.old;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.adapter.ContactAdapter;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseFragment;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.bean.WechatContactBean_;
import com.xmb.wechat.contact.ContactUtils;
import com.xmb.wechat.contact.QuickIndexBar;
import com.xmb.wechat.definterface.EmptyDeliver;
import com.xmb.wechat.view.wechat.chat.WechatDanliaoDetailActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 通讯录
 */
public class ContactsFragment extends BaseFragment implements ContactAdapter.NotifyUpdate {
    @BindView(R2.id.expandable_listview)
    ExpandableListView expandableListview;
    @BindView(R2.id.quick_index_bar)
    QuickIndexBar      quickIndexBar;
    @BindView(R2.id.text_dialog)
    TextView           textDialog;
    @BindView(R2.id.ll_hint)
    LinearLayout       mLlHint;

    private View    footerView;
    private boolean mIsNeedFeedBack;

    public ContactsFragment() {
        super(R.layout.fragment_contacts);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null)
            mIsNeedFeedBack = arguments.getBoolean("isNeedFeedBack");
    }

    @OnClick(R2.id.iv_close_hint)
    public void onViewClicked() {
        mLlHint.setVisibility(View.GONE);
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    /**
     * 获取数据
     */
    public void getData() {
        final ArrayList<ArrayList<WechatContactBean>> groupList = ContactUtils.getSortContactList();
        //FooterView,统计联系人：
        List<WechatContactBean> dataList = WechatStartApplication.getBoxStore().boxFor(WechatContactBean.class).query()
                .equal(WechatContactBean_.isInner, false).build().find();
        if (footerView != null)
            expandableListview.removeFooterView(footerView);
        footerView = inflater.inflate(R.layout.wechat_contact_item_footer_view, null, false);
        TextView tvCount = footerView.findViewById(R.id.tv_contact_count);
        tvCount.setText(dataList.size() + "位联系人");
        footerView.setOnClickListener(null);
        expandableListview.addFooterView(footerView);

        /**设置适配器*/
        expandableListview.setAdapter(new ContactAdapter(getActivity(), this, groupList, new ContactAdapter.OnContactItemClick() {
            @Override
            public void onItemClick(WechatContactBean wechatContactBean) {
                if (!mIsNeedFeedBack) {
                    showExemption(new EmptyDeliver() {
                        @Override
                        public void onDeliver() {
                            WechatDanliaoDetailActivity.start(getActivity(), wechatContactBean);
                        }
                    });
                } else {
                    //                    WechatDanliaoActivity activity = (WechatDanliaoActivity) getActivity();
                    //                    if (activity != null)
                    //                        activity.finishFeedBack(wechatContactBean);
                }
            }
        }));
        /**设置group不可点击*/
        expandableListview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;//注意：一定要返回true，返回true这个方法才有效。
            }
        });
        /**展开所有条目*/
        for (int i = 0; i < groupList.size(); i++) {
            expandableListview.expandGroup(i);
        }

        //设置导航字母：
        quickIndexBar.setTextView(textDialog);//设置textDialog
        quickIndexBar.setPaddingTop(15);//设置顶部padding
        quickIndexBar.setPaddingBottom(15);//设置底部padding

        /**设置监听器*/
        quickIndexBar.setOnLetterChangedListener(new QuickIndexBar.OnLetterChangedListener() {
            @Override
            public void onLetterChanged(String letter) {
                for (int i = 0; i < groupList.size(); i++) {
                    if (letter.equals(groupList.get(i).get(0).getFirstLetter())) {
                        expandableListview.setSelectedGroup(i);
                    }
                }
            }

            @Override
            public void onLetterGone() {
            }
        });

    }

    @Override
    public void updateData() {
        getData();
    }
}
